<?php


namespace App\Models\CacheModel\live;


use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class LiveAnchorAdmin extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_anchor_admin';
    protected $cacheModel = "live.anchor_admin";

    /**
     * 获取管理的主播 直播间ID
     * @param int $uid
     * @return array
     */
    public function getAdminAnchorLiveId(int $uid): array
    {
        try
        {
            $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->getDataAndSet(function () use ($uid)
            {
                try
                {
                    $liveIds = $this->where(['uid' => $uid, 'status' => 1])->column('live_id');

                    return json_encode($liveIds);
                }
                catch (\Exception $e)
                {
                    return '[]';
                }
            });

            return $data ? json_decode($data, true) : [];
        }
        catch (\Exception $e)
        {
            Log::write("LiveAnchorAdmin获取管理的主播 异常：{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()}", 'error', true);

            return [];
        }
    }

    /**
     * 获取用户是不是 主播管理员
     * @param int $uid
     * @return int  // 0=>不是主播管理员  1=>是主播管理员
     */
    public function getUserIsLiveAdmin(int $uid): int
    {
        $liveIds = $this->getAdminAnchorLiveId($uid);

        return $liveIds ? 1 : 0;
    }

    /**
     * 主播管理员获取 我的主播列表
     * @param int $uid
     * @return array
     */
    public function getMyLivesData(int $uid): array
    {
        $liveInfoList = $this->alias('la')->join('wechat_backup_07_18_14_43_00.live_anchor_info a', 'la.live_id=a.live_id')
            ->where(['la.uid' => $uid, 'la.status' => 1])->column('a.name,a.avatar', 'la.live_id');

        $resData = [];
        $time = time();
        foreach ($liveInfoList as $liveId => $liveInfoItem)
        {
            $liveData = [
                'live_id' => (int)$liveId,
                'nickname' => $liveInfoItem['name'],
                'avatar' => $liveInfoItem['avatar'],
            ];
            $recordId = (new LiveRecordingInfo())->getNewestRecord($liveId);
            $record = (new LiveRecordingInfo())->getInfoById($recordId);
            if ((int)$record['status'] === 1)
            {   // 直播中
                $liveData['status'] = 1;
                $liveData['desc'] = '已开播：' . Sec2Time($time - $record['live_start_time']);
            } else
            {   // 未开播
                $broadcastPreviewInfo = $this->table('wechat_backup_07_18_14_43_00.live_broadcast_preview')->where('live_id', $liveId)
                    ->where('live_start_time', '>', time())->order('id asc')->field('live_start_time')->find();

                $liveData['status'] = 0;
                if ($broadcastPreviewInfo)
                {
                    $liveData['desc'] = '预告开播时间：' . date('Y m.d H:i', $broadcastPreviewInfo['live_start_time']);
                } else
                {
                    $liveData['desc'] = '预告开播时间：暂无';
                }
            }

            $resData[] = $liveData;
        }

        return $resData;
    }
}
